local zuocui = fk.CreateSkill{
  name = "xiaobai__zuocui"
}

Fk:loadTranslationTable{
  ["xiaobai__zuocui"] = "佐榱",
  [":xiaobai__zuocui"] = "出牌阶段，你可以对一名手牌数与装备区牌数之差恰好为你本阶段造成伤害量的角色造成1点伤害，"..
    "然后其可以弃置一张♠牌并回复1点体力，令你选择一项：1.摸两张牌；2.获得其场上一张牌并转换“墨撰”至下一项。",
  
  ["#xiaobai__zuocui"] = "佐榱：你可以对一名手牌数与装备区牌数之差恰好为你本阶段造成伤害量的角色造成1点伤害",
  ["@xiaobai__zuocui_damage-phase"] = "佐榱",
  ["xiaobai__zuocui_active"] = "佐榱",
  ["#xiaobai__zuocui_active"] = "佐榱：你可以弃置一张♠牌并回复1点体力",
  ["xiaobai__zuocui_draw"] = "摸两张牌",
  ["xiaobai__zuocui_aquire"] = "获得 %dest 场上一张牌并转换“墨撰”",
}

---@param mark string
local nextStage = function (mark)
  local m = {"①","②","③","④"}
  local index = table.indexOf(m, mark)
  if index < 1 or index > 4 then return m[2] end
  return m[index % 4 + 1]
end

zuocui:addEffect("active", {
  prompt = "#xiaobai__zuocui",
  can_use = function (self, player)
    local damage_num = player:getMark("@xiaobai__zuocui_damage-phase")
    local targets = table.filter(Fk:currentRoom().alive_players, function(p)
      return math.abs(p:getHandcardNum()-#p:getCardIds("e")) == damage_num
    end)
    return #targets > 0
  end,
  target_filter = function (self, player, to_select, selected, selected_cards)
    if #selected > 0 then return end
    local damage_num = player:getMark("@xiaobai__zuocui_damage-phase")
    local targets = table.filter(Fk:currentRoom().alive_players, function(p)
      return math.abs(p:getHandcardNum()-#p:getCardIds("e")) == damage_num
    end)
    return table.contains(targets, to_select)
  end,
  feasible = function (self, player, selected, selected_cards, card)
    return #selected > 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function (self, room, skillUseEvent)
    local player = skillUseEvent.from
    local target = skillUseEvent.tos[1]
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = self.name,
    }
    if target.dead then return end
    local _, ret = room:askToUseActiveSkill(target, {
      skill_name = "xiaobai__zuocui_active",
      cancelable = true,
      prompt = "#xiaobai__zuocui_active"
    })
    if not ret then return end
    local cards = ret.cards
    room:throwCard(cards, self.name, target, player)
    if target.dead then return end
    room:recover{
      recoverBy = player,
      num = 1,
      skillName = self.name,
      who = target,
    }
    if player.dead then return end
    local all_choices = {
        "xiaobai__zuocui_draw",
        "xiaobai__zuocui_aquire::"..target.id
    }
    local choices = {
      "xiaobai__zuocui_draw",
    }
    if not target.dead and #target:getCardIds("ej")>0 then
      table.insert(choices, all_choices[2])
    end
    local choice = room:askToChoice(player, {
      all_choices = all_choices,
      choices = choices,
      cancelable = false,
      skill_name = self.name,
    })
    if not choice then return end
    if choice == all_choices[1] then
      player:drawCards(2, self.name)
    else
    local card = room:askToChooseCard(player, {
      flag = "ej",
      target = target,
      skill_name = self.name,
      cancelable = false
    })
    room:obtainCard(player, card, true, fk.ReasonPrey, player, self.name)
    if player.dead or not player:hasSkill("xiaobai__mozhuan", true, true) then return end
    player.room:setPlayerMark(player, "@xiaobai__mozhuan", nextStage(player:getMark("@xiaobai__mozhuan")))
    end
  end
})

zuocui:addEffect(fk.Damaged, {
  can_refresh = function (self, event, target, player, data)
    return data.from == player and player:hasSkill(self, true, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@xiaobai__zuocui_damage-phase", data.damage)
  end
})

zuocui:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local damageNum = 0
  room.logic:getActualDamageEvents(1, function (e)
    if e.data.from and e.data.from == player then
      damageNum = damageNum + e.data.damage
    end
    return false
  end, Player.HistoryPhase)
  room:setPlayerMark(player, "@xiaobai__zuocui_damage-phase", damageNum)
end)

return zuocui
